using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class FPButton
    : MonoBehaviour,
        IPointerEnterHandler,
        IPointerExitHandler,
        IPointerClickHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IPointerMoveHandler
{
    public UnityEvent onEnter;
    public UnityEvent onDown;
    public UnityEvent onUp;
    public UnityEvent onClick;
    public UnityEvent onExit;
    public UnityEvent<PointerEventData> onMove;

    private Image highLightImage;
    public AudioClip clickSound;
    private AudioSource clickSource;

    private void Awake()
    {
        highLightImage = transform.Find("Highlight").GetComponent<Image>();
        if (highLightImage)
            highLightImage.enabled = false;
        if (clickSound)
        {
            clickSource = gameObject.AddComponent<AudioSource>();
            clickSource.clip = clickSound;
        }
    }

    private void OnEnable()
    {
        if (highLightImage)
            highLightImage.enabled = false;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (highLightImage)
            highLightImage.enabled = true;
        onEnter?.Invoke();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (highLightImage)
            highLightImage.enabled = false;
        onExit?.Invoke();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (clickSource)
            clickSource.Play();
        onClick?.Invoke();
    }

    private void Reset()
    {
#if UNITY_EDITOR
        clickSound = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(
            "Assets/RGBHand/Core/AuidoClips/BtnClick.mp3"
        );
#endif
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        onUp?.Invoke();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        onDown?.Invoke();
    }

    public void OnPointerMove(PointerEventData eventData)
    {
        onMove?.Invoke(eventData);
    }
}
